﻿using System;
using Google.GData.Client;
using Informant.Engines.ErrorHandling;

namespace Informant.Engines.Google.Contacts
{
	public class ContactsErrorHandler : ErrorHandler
	{
		/// <summary>
		/// Handles the error by taking application specific and external exceptions and throwing a client friendly exception.
		/// </summary>
		/// <param name="exception">The exception.</param>
		/// <exception cref="System.Security.Authentication.AuthenticationException"/>
		/// <exception cref="System.Net.WebException"/>
		/// <exception cref="System.InvalidOperationException"/>
		public override void MakeExceptionClientFriendly(Exception exception)
		{
			if (exception is GDataRequestException || exception is InvalidCredentialsException)
			{
				//TODO: Logger.Write("AuthenticationException: " + ErrorResources.AuthenticationExceptionInvalidCredentialsOrTargetServerAddress + Environment.NewLine + exception.StackTrace);
				throw new System.Security.Authentication.AuthenticationException(
					ErrorResources.AuthenticationExceptionInvalidCredentialsOrTargetServerAddress);
			}
			else if (exception is CaptchaRequiredException)
			{
				//TODO: Logger.Write("AuthenticationException: " + ErrorResources.AuthenticationExceptionAccountLocked + Environment.NewLine + exception.StackTrace);
				throw new System.Security.Authentication.AuthenticationException(
					String.Format(ErrorResources.AuthenticationExceptionAccountLocked, ((CaptchaRequiredException)exception).Url));
			}
			else if (exception is AccountDisabledException)
			{
				//TODO: Logger.Write("AuthenticationException: " + ErrorResources.AuthenticationExceptionInvalidCredentialsOrTargetServerAddress + Environment.NewLine + exception.StackTrace);
				throw new System.Security.Authentication.AuthenticationException(
					ErrorResources.AuthenticationExceptionInvalidCredentialsOrTargetServerAddress);
			}
			else
			{
				base.MakeExceptionClientFriendly(exception);
			}
		}
	}
}
